const oldArrayProtoMethods = Array.prototype;

export let ArrayMethods = Object.create(oldArrayProtoMethods);

const methods = ['push', 'pop', 'unshift', 'shift', 'splice', 'reserve', 'sort'];

methods.forEach((method) => {
  ArrayMethods[method] = function (...args) {
    // 调用 Array 的原始方法实现功能
    let result = oldArrayProtoMethods[method].apply(this, args);

    // 劫持数组新增的数据
    let inserted;
    switch (method) {
      case 'push':
      case 'unshift':
        inserted = args;
        break;
      case 'splice':
        inserted = args.splice(2);
    }
    let ob = this.__ob__;
    if (inserted) {
      ob.observerArray(inserted);
    }
    ob.dep.notify();
    return result;
  };
});
